//
//  ALDBManage+DBUpdate.m
//  ios-loan-assistant
//
//  Created by hcs on 2018/9/11.
//  Copyright © 2018年 guoqingping. All rights reserved.
//

#import "ALDBManage+DBUpdate.h"
#import "ALDocPathOrUrlUtils.h"
//#import "HSComAction.h"
#import "ALDBManageMacro.h"
const static NSInteger FAMILY_LIST_DB_MANAGER_VER = 1;

@implementation ALDBManage (DBUpdate)
#pragma mark 检查数据库更新  start===================
- (void)checkSqlUpdate{
    // 升级操作
    NSInteger ver = [[NSUserDefaults standardUserDefaults] integerForKey:@"familyListDBManagerVersion"];
    if (ver < FAMILY_LIST_DB_MANAGER_VER) {
        [self upgrade];
    }
}
- (void)upgrade {
    //获取存储好的原版本号
    NSInteger oldVersionNum = [[NSUserDefaults standardUserDefaults] integerForKey:@"familyListDBManagerVersion"];
    if (FAMILY_LIST_DB_MANAGER_VER <= oldVersionNum) {
        return;
    }
    
    // 保存新的版本号到库中 -这里大家可以使用NSUserDefault存储
    [[NSUserDefaults standardUserDefaults] setInteger:FAMILY_LIST_DB_MANAGER_VER forKey:@"familyListDBManagerVersion"];
    
    //升级
    [self upgrade:oldVersionNum];
    
}
- (void)upgrade:(NSInteger)oldVersion {
    if (oldVersion >= FAMILY_LIST_DB_MANAGER_VER) {
        return;
    }
    switch (oldVersion) {
        case 0:
            [self upgradeFrom0To1];
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        default:
            break;
    }
    oldVersion ++;
    
    // 递归判断是否需要升级
    [self upgrade:oldVersion];
}
- (void)upgradeFrom0To1 {
    //这里执行Sql语句 执行版本1到版本2的更新
    NSDictionary *userDic=[ALDBManage getLoginUser2];
    [self reCreateTable:kUserTableName];
    
    NSString *sql=[NSString stringWithFormat:@"Insert Into %@ ('userId','nickName','userName','mobilePhone','icon','sex','stageJsonBiao','token','timeOut','IsLogin') Values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%d')",kUserTableName,userDic[@"userId"],userDic[@"nickName"],userDic[@"userName"],userDic[@"mobilePhone"],userDic[@"icon"],userDic[@"sex"],userDic[@"stageJsonBiao"],userDic[@"token"],userDic[@"timeOut"],[userDic[@"isLogin"] intValue]];
    NSString *path= [ALDocPathOrUrlUtils documentsPath:@""];
    NSLog(@"docPath=%@",path);
#warning 这里还待优化，此处是有问题的
    BOOL isSuc= [[ALDBManage standarCacheDBManage] insertWithSQLStr:sql];
    if (isSuc) {
        NSLog(@"成功");
    }
}

+(id)getLoginUser2{
    ALDBManage *dbManage = [ALDBManage standarDBManage];
    NSString *sql = [NSString stringWithFormat:@"Select * From %@ where isLogin = 1",kUserTableName];
    FMResultSet *rs = [dbManage selectWithSQLStr:sql];
    NSMutableDictionary *recordDic = [NSMutableDictionary dictionary];
    while ([rs next]) {
        for (NSInteger i = 0; i < rs.columnCount; i ++) {
            NSString *key = [rs columnNameForIndex:i];
            NSString *valus = [NSString stringWithFormat:@"%@",[rs stringForColumn:key]];
            [recordDic setObject:valus forKey:key];
        }
    }
    NSMutableDictionary *userMulDic=[NSMutableDictionary dictionaryWithDictionary:recordDic];
    if ([userMulDic.allKeys count]>0) {
        [userMulDic setObject:recordDic[@"userId"] forKey:@"id"];
    }
    return  userMulDic;
}
#pragma mark 检查数据库更新  end===================
@end
